export default {
	namespaced: true,
	state: {
		address: uni.getStorageSync('address'),
		token: uni.getStorageSync('token'), // 登录的凭证
		userInfo: uni.getStorageSync('userInfo'),
		// 重定向的 object 对象 { openType, from }
		redirectInfo: null
	},
	getters: {
		address(state) {
			return state.address
		},
		addstr(state) {
			if (!state.address) return ''
			return state.address.provinceName + state.address.cityName + state.address.countyName + state.address
				.detailInfo
		},
		getToken(state) {
			return state.token
		},
		getUserInfo(state) {
			return state.userInfo
		},
		getRedirectInfo(state) {
			return state.redirectInfo
		}
	},
	mutations: {
		// 更新收货地址
		updateAddress(state, address) {
			state.address = address

			// 2. 通过 this.commit() 方法，调用 m_user 模块下的 saveAddressToStorage 方法将 address 对象持久化存储到本地
			this.commit('m_user/saveAddressToStorage')
		},
		saveAddressToStorage(state) {
			uni.setStorageSync('address', state.address)
		},
		updateUserInfo(state, userInfo) {
			state.userInfo = userInfo

			// 把 userInfo 保存到本地
			this.commit('m_user/saveUserInfoToStorage')
		},
		saveUserInfoToStorage(state) {
			uni.setStorageSync('userInfo', state.userInfo)
		},
		updateToken(state, token) {
			state.token = token

			// 把token 保存到本地
			this.commit('m_user/saveTokenToStorage')
		},
		saveTokenToStorage(state) {
			uni.setStorageSync('token', state.token)
		},
		// 更新重定向的信息对象
		updateRedirectInfo(state, info) {
			state.redirectInfo = info
		}
	}
}
